package priv.pfz.basedao.mybatis.sqlprovider;

import org.apache.ibatis.builder.annotation.ProviderContext;
import priv.pfz.basedao.Utils;
import priv.pfz.basedao.info.FieldInfo;
import priv.pfz.basedao.info.DaoInfo;
import priv.pfz.basedao.mybatis.DaoInfoCache;
import priv.pfz.basedao.sharding.ShardingInfo;
import priv.pfz.basedao.sharding.ShardingManager;
import priv.pfz.basedao.sqlbuilder.SQL;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * UPDATE table SET a=entity.a, b=entity.b
 * WHERE (b=example1.b AND c=example1.c) OR (c=example2.c AND d=example2.d) OR ...
 * @author yyjxp
 * 2024/1/28 23:55
 */
public class UpdateByExamplesSqlProvider extends BaseSqlProvider {

    public String provideSql(Object entity, Iterable<?> examples, ProviderContext context) {
        return innerProvideSql(entity, examples, null, context);
    }

    public String provideShardingSql(Object entity, Iterable<?> examples, ShardingInfo shardingInfo, ProviderContext context) {
        return innerProvideSql(entity, examples, shardingInfo, context);
    }

    private String innerProvideSql(Object entity, Iterable<?> examples, ShardingInfo shardingInfo, ProviderContext context) {
        Utils.checkNotNull(entity, "entity不能为空");
        Utils.checkNotEmpty(examples, "examples不能为空");
        DaoInfo daoInfo = DaoInfoCache.getDaoInfo(context.getMapperType());
        Map<String, Object> valueMap = getValueMap(daoInfo, entity);
        Utils.checkNotEmpty(valueMap, "entity字段不能全部为空");
        ShardingManager shardingManager = new ShardingManager(daoInfo, shardingInfo);

        SQL sql = new SQL();
        for (FieldInfo fieldInfo : daoInfo.getFieldInfos()) {
            if (!valueMap.containsKey(fieldInfo.getFieldName())) {
                continue;
            }
            sql.SET(getFieldEquals("arg0", fieldInfo));
        }
        List<String> allConditions = new ArrayList<>();
        Iterator<?> iterator = examples.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            Object example = iterator.next();
            List<String> conditions = new ArrayList<>();
            Map<String, Object> exampleValueMap = getValueMap(daoInfo, example);
            Utils.checkNotEmpty(exampleValueMap, "examples[{0}]字段不能全部为空", i);
            for (FieldInfo fieldInfo : daoInfo.getFieldInfos()) {
                if (!exampleValueMap.containsKey(fieldInfo.getFieldName())) {
                    continue;
                }
                conditions.add(getFieldEquals("arg1", i, fieldInfo));
                shardingManager.addShardingInfo(fieldInfo, exampleValueMap.get(fieldInfo.getFieldName()));
            }
            allConditions.add(String.join(" AND ", conditions));
        }
        sql.WHERE(String.join(" OR ", allConditions));
        if (daoInfo.isEnableLogicDelete()) {
            sql.AND();
            sql.WHERE(getLogicDeleteWhere(daoInfo));
        }
        sql.UPDATE(shardingManager.getShardingTable());
        String sqlStr = sql.toString();
        sqlStr += shardingManager.getThirdShardingStr();
        System.out.println(sqlStr);
        return sqlStr;
    }
}
